// pages/organization/join.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    org_id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var org_id = options.org_id;
    console.log(org_id);
  this.setData({
    org_id: org_id,
  })
    wx.setNavigationBarTitle({
      title: "加入组织",
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var org_id = this.data.org_id;
    var that=this;
    if(org_id){
      wx.request({
        url: app.globalData.default_url,
        data: {
          action: 'api.v1.organization.search',
          org_id: org_id,
        },
        success:function(res){
          if(res.data.op=="fail"){
            wx.showModal({
              title: '失败',
              content: res.data.reason,
              confirmText:"重试",
              success:function(res){
                console.log(res.data);
                if(res.confirm){
                  that.onShow();
                  return;
                }else{
                  wx.navigateBack({
                    
                  })
                }
              }
              })
          }else{
            that.setData({
              org:res.data.data,
            })
          }
        },
        fail:function(res){
          wx.showModal({
            title: '网络错误',
            content: '请稍后重试，或联系管理员',
            confirm:"重试",
            success:function(res){
              if(res.confirm){
                if(res.confirm){
                  that.onShow();
                  return;
                }else{
                  wx.navigateBack({
                    
                  })
                }
              }
            }
          })
        },
        complete:function(res){

        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //申请加入组织
  join_request: function (e) {
    var yuyue_session = wx.getStorageSync("yuyue_session");
    console.log(e);
    if (e.detail.value){
      console.log(e.detail.value.org_id);
      var org_id = e.detail.value.org_id;
      var org_password = e.detail.value.org_password;
    }else{
      var org_id = e.currentTarget.dataset.org_id;
      var org_password = e.currentTarget.dataset.password; 
    }
    
   

    wx.request({
      url: app.globalData.default_url,
      data: {
        action: 'api.v1.organization.join',
        yuyue_session: yuyue_session,
        org_password: org_password,
        org_id: org_id,

      },
      success: function (res) {
        console.log(res);
        var isFail = res.data.op == "fail";
        if (isFail){
          var title ="申请失败" ;
          var content = res.data.reason ;
          var showCancel = true;
          var confirmText = "重试";
        }else{
          var title =  "申请成功";
          var content =  "请耐心等待";
          var showCancel = false;
          var confirmText = "确定";
        }
      
        wx.showModal({
          title: title,
          content: content,
          showCancel: showCancel,
          cancelText: "取消",
          confirmText: confirmText,
          success: function (r) {
            if (r.confirm && isFail) {
              console.log("单击重试");
            } else {
              wx.navigateBack({

              })
            }
          }
        })

      },
      fail: function () {
        wx.showModal({
          title: "连接错误",
          content: "请检查网络连接",
          cancelText: "取消",
          confirmText: "重试",
          success: function (r) {
            if (r.confirm) {
              console.log("单击重试");
            } else if (r.cancel) {
              wx.navigateBack({

              })
            }
          }
        })
      }

    })
  },
})